<?php

	/**
	 * @see Bike_Js_ResponseHandler
	 */
	require_once 'Bike/Js/ResponseHandler.php';
	
	
	class Bike_Js_Ext_ResponseHandler_Success extends Bike_Js_ResponseHandler
	{
				
		protected function initDefaultSettings()
		{
			parent::initDefaultSettings();
		
			$js = $this->js();
			$this->setSettings(array(
				'args' => array($js->object, $js->response),
				'body' => $this->getJsBody(),
				'bodyDebugCode' => array(
					$js->alert('Success'),
					$this->bodyDebugCode,
				),
			));
		}
		
		/**
		 * Get JS body
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsBody()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__,
					$js->CodeContainer(array(
						$js->_var($js->object)->_val($js->object, $js->this),
						$js->_var($js->success)->_val(False),
						$js->TryCatch
							->_tryBody(array(							
								($js->isDebug()
									? $js->for($js->i->_in($js->response))->_body(array(
										$js->alert($js->i->_add(' => ')->_add($js->response[$js->i]))
									)) : ''),
								
								$js->_var($js->Response)->_val($js->Ext->JSON->decode($js->response->responseText)),
								$js->success->_val($js->Response->success),
							)),
						$js->if($js->response->status->_neq(200)->_or($js->_not($js->success)))
							->_body($js->return($js->Ext->ResponseHandler->Failure->call($js->this, $js->object, $js->response))),
						$js->if($js->object->loadMask)
	                    	->_body($js->object->loadMask->hide()),
	                    $js->return($js->Response->handler->call($js->object)),
					))
				);
			}
			return $this->getParamCache(__METHOD__);
		}
	}
